<?php
/* 
 * 文件名 class.utility.php
 * 文件说明 公共函数库
 * @version 0.1
 * @author samuel
 * @date 2009-05-06
 */

class utility {

    private $core;

    function  __construct($core) {
        $this->core = $core;
    }

    //判断管理员是否登录状态
    public function checkLoginStatus(){
        if($_SESSION['admin_username']){
            return true;
        }else{
            return false;
        }
    }

    //列表分页
    public function pager($tablename, $countfield, $append_where, $arr_limit){
        $sql = "SELECT count(a.`".$countfield."`) FROM `".$tablename."` as a WHERE 1 " . $append_where;
        $total_count = $this->core->db->get_var($sql);
        $current_page = $arr_limit['page'];
        $pagesize = $arr_limit['pagesize'];
        $page_url = $arr_limit['url'];
        $total_page = ceil($total_count/$pagesize);
		if($total_page < 1) $total_page = 1;
        $pre_page = $current_page > 1 ? ($current_page - 1) : 1;
        $next_page = ($current_page + 1) > $total_page ? $total_page : ($current_page + 1);
        $first_page_url = $this->appendParamToUrl($page_url, 'page', 1, $base_url);
        $last_page_url = $this->appendParamToUrl($page_url, 'page', $total_page);
        $pre_page_url = $this->appendParamToUrl($page_url, 'page', $pre_page);
        $next_page_url = $this->appendParamToUrl($page_url, 'page', $next_page);
        $pager = array(
            'current_page'      => $current_page,
            'total_page'        => $total_page,
            'first_page'        => $first_page_url,
            'last_page'         => $last_page_url,
            'pre_page'          => $pre_page_url,
            'next_page'         => $next_page_url,
            'page_url'          => $base_url,
        );
        return $pager;
    }

    //构造URL，在给定的URL后附加参数
    public function appendParamToUrl($url, $param, $value, &$base_url=""){
        if(!$url) return false;
        $url_info = parse_url($url);
        $url_query_string = $url_info['query'];
        parse_str($url_query_string, &$url_params);
        //获取基本URL
        $base_url_params = $url_params;
        unset($base_url_params[$param]);
        $base_url_query_string = http_build_query($base_url_params);
        $base_url = $url_info['scheme'] . "://" . $url_info['host'] . $url_info['path'] . "?" . $base_url_query_string;
        //获取新的URL
        $base_url_params[$param] = $value;
        $new_url_query_string = http_build_query($base_url_params);
        $new_url = $url_info['scheme'] . "://" . $url_info['host'] . $url_info['path'] . "?" . $new_url_query_string;
        return $new_url;
    }

    /**
   * 页面信息提示
   * @param <string> $msg           信息内容
   * @param <string> $backurl       返回地址
   * @param <int> $autoreturn    是否自动返回：0否，1是
   * @param <int> $type          类型：0,JS，1,页面
   */
	public function messagePrompt($msg = '', $backurl = '', $autoreturn = 1, $type = 0){
		$msg_prompt = "";
		if($type == 0)
		{
			$msg_prompt .= "<script language=javascript>alert('" . $msg . "');";
			if($autoreturn && $backurl)
			{
				$msg_prompt .= "window.location.href = '" . $backurl . "';";
			}
			elseif($autoreturn && !$backurl)
			{
				$msg_prompt .= "history.go(-1);";
			}
			$msg_prompt .= "</script>";
		}
		else
		{
			$msg_prompt .= $msg . "<br/>";
			if(!$backurl) $backurl = $_SERVER['HTTP_REFERER'];
			if($autoreturn){
				$msg_prompt .= "<br/>This page will redirect.<a href='" . $backurl . "'>Click here.</a>";
				$msg_prompt .= "<script language=javascript>setTimeout(\"window.location.href='" . $backurl . "'\", 3000);</script>";
			}
			else
			{
				$msg_prompt .= "<a href='" . $backurl . "'>Click here.</a>";
			}
		}

		echo  $msg_prompt;
	}
	
	/**
	 * WAP分页
	 */
	function wap_pager($total_pages, $current_page, $display_num, $base_url){
        if(!$total_pages || !$current_page || ($total_pages < $current_page)) return false;
        if(!$display_num) $display_num = 10;
        $before_num = floor($display_num/2);
        $after_num = ceil($display_num/2);
        $first_page = 1;
        $last_page = $total_pages;
        $pre_page = $current_page - 1 > 0 ? $current_page - 1 : 1;
        $next_page = $current_page + 1 > $total_pages ? $total_pages : $current_page + 1;
        $str_pager = "";
        #$str_pager .= "<a href='".$base_url."&amp;page=".$pre_page."'>&lt;&lt;Previous</a>";
        if($total_pages <= $display_num){
            for($i = 1; $i <= $total_pages; $i++){
                $class = "";
                #if($current_page == $i) $class = "class='page_view'";
                $str_pager .= "<a $class href='".$base_url."&amp;page=".$i."'>".$i."</a>";
            }
        }elseif(($current_page - $before_num) > 1 && ($current_page + $after_num + 1 >= $total_pages)){
            $str_pager .= "<a href='".$base_url."&amp;page=1'>1</a><a>…</a>";
            for($i = $current_page - $before_num; $i <= $total_pages; $i++){
                $class = "";
                #if($current_page == $i) $class = "class='page_view'";
                $str_pager .= "<a $class href='".$base_url."&amp;page=".$i."'>".$i."</a>";
            }
        }elseif(($current_page - $before_num <= 2) && ($current_page + $after_num < $total_pages)){
            for($i = 1; $i <= $display_num; $i++){
                $class = "";
                #if($current_page == $i) $class = "class='page_view'";
                $str_pager .= "<a $class href='".$base_url."&amp;page=".$i."'>".$i."</a>";
            }
            $str_pager .= "<a>…</a><a href='".$base_url."&amp;page=".$total_pages."'>".$total_pages."</a>";
        }else{
            $str_pager .= "<a href='".$base_url."&amp;page=1'>1</a><a>…</a>";
            for($i = $current_page - $before_num; $i <= $current_page + $after_num; $i++){
                $class = "";
                #if($current_page == $i) $class = "class='page_view'";
                $str_pager .= "<a $class href='".$base_url."&amp;page=".$i."'>".$i."</a>";
            }
            $str_pager .= "<a>…</a><a href='".$base_url."&amp;page=".$total_pages."'>".$total_pages."</a>";
        }
        $str_pager .= "<a href='".$base_url."&amp;page=".$next_page."'>&gt;&gt;</a>";

        return $str_pager;
    }

}
?>
